home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1991 / 05 / d_flat / editbox.c < prev    next >
Text File  |  1991-02-19  |  25KB  |  987 lines

  1. /* ------------- editbox.c ------------ */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include "dflat.h"
  8.  
  9. #define EDITBUFFERLENGTH  4096
  10. #define ENTRYBUFFERLENGTH 1024
  11. #define GROWLENGTH        1024
  12.  
  13.  
  14. #define EditBufLen(wnd) (isMultiLine(wnd) ? EDITBUFFERLENGTH : ENTRYBUFFERLENGTH)
  15. #define WndCol (wnd->CurrCol-wnd->wleft)
  16. #define CurrChar (wnd->CurrLine+wnd->CurrCol)
  17.  
  18. static void PasteText(WINDOW, char *, int);
  19. static void SaveDeletedText(WINDOW, char *, int);
  20. static void SetAnchor(WINDOW, int, int);
  21. static void Forward(WINDOW);
  22. static void Backward(WINDOW);
  23. static void End(WINDOW);
  24. static void Home(WINDOW);
  25. static void Downward(WINDOW);
  26. static void Upward(WINDOW);
  27. static char *FindLinePointer(WINDOW, int);
  28. static void StickEnd(WINDOW);
  29. static void UpLine(WINDOW);
  30. static void DownLine(WINDOW);
  31. static void NextWord(WINDOW);
  32. static void PrevWord(WINDOW);
  33. static void ResetEditBox(WINDOW);
  34. #define SetLinePointer(wnd, ln)    (wnd->CurrLine = FindLinePointer(wnd, ln))
  35.  
  36.  
  37. static int KeyBoardMarking;
  38. int TextMarking;
  39. static int dragcount = -1;
  40.  
  41. char *Clipboard;
  42. int ClipboardLength;
  43.  
  44.  
  45. int EditBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  46. {
  47.     int rtn;
  48.     static int py = -1;
  49.     int kx = (int) p1 - GetLeft(wnd);
  50.     int ky = (int) p2 - GetTop(wnd);
  51.     int c;
  52.     RECT rc = ClientRect(wnd);
  53.     char *lp;
  54.     int len;
  55.  
  56.     int    mx;
  57.     int    my;
  58.  
  59.     mx = (int) p1 - GetClientLeft(wnd) + wnd->wleft;
  60.     my = (int) p2 - GetClientTop(wnd) + wnd->wtop;
  61.     if (my < 0)
  62.         my = 0;
  63.  
  64.     switch (msg)    {
  65.         case CREATE_WINDOW:
  66.             rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  67.             wnd->text = malloc(EditBufLen(wnd));
  68.             wnd->textlen = EditBufLen(wnd);
  69.             ResetEditBox(wnd);
  70.             return rtn;
  71.         case ADDTEXT:
  72.             rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  73.             if (!isMultiLine(wnd))    {
  74.                 wnd->CurrLine = wnd->text;
  75.                 wnd->CurrCol = strlen((char *)p1);
  76.                 if (wnd->CurrCol >= ClientWidth(wnd))    {
  77.                     wnd->wleft = wnd->CurrCol - ClientWidth(wnd);
  78.                     wnd->CurrCol -= wnd->wleft;
  79.                 }
  80.                 wnd->BlkEndCol = wnd->CurrCol;
  81.                 PostMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  82.             }
  83.             return rtn;
  84.         case SETTEXT:
  85.             rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  86.             wnd->CurrLine = wnd->text;
  87.             return rtn;
  88.         case CLEARTEXT:
  89.             ResetEditBox(wnd);
  90.             break;
  91.         case EB_GETTEXT:    {
  92.             char *cp1 = (char *)p1;
  93.             char *cp2 = wnd->text;
  94.             while (p2-- && *cp2 && *cp2 != '\n')
  95.                 *cp1++ = *cp2++;
  96.             *cp1 = '\0';
  97.             return TRUE;
  98.         }
  99.         case EB_PUTTEXT:
  100.             if (!isMultiLine(wnd))    {
  101.                 SendMessage(wnd, CLEARTEXT, 0, 0);
  102.                 SendMessage(wnd, ADDTEXT, p1, p2);
  103.             }
  104.             return TRUE;
  105.         case SETFOCUS:
  106.             rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  107.             if (p1)    {
  108.                 SendMessage(NULLWND, SHOW_CURSOR, GetCommandToggle(ID_INSERT), 0);
  109.                 SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  110.             }
  111.             else
  112.                 SendMessage(NULLWND, HIDE_CURSOR, 0, 0);
  113.             return rtn;
  114.         case SHIFT_CHANGED:
  115.             if (!(p1 & (LEFTSHIFT | RIGHTSHIFT)) && KeyBoardMarking)    {
  116.                 SendMessage(wnd, BUTTON_RELEASED, 0, 0);
  117.                 KeyBoardMarking = FALSE;
  118.             }
  119.             break;
  120.         case DOUBLE_CLICK:
  121.             if (KeyBoardMarking)
  122.                 return TRUE;
  123.             break;
  124.         case LEFT_BUTTON:
  125.             if (KeyBoardMarking || TextMarking)
  126.                 return TRUE;
  127.             if (WindowMoving || WindowSizing)
  128.                 break;
  129.             if (!InsideRect(p1, p2, rc))
  130.                 break;
  131.  
  132.             if (isMultiLine(wnd))    {
  133.  
  134.                 if (BlockMarked(wnd))    {
  135.                     ClearBlock(wnd);
  136.                     SendMessage(wnd, PAINT, 0, 0);
  137.                 }
  138.  
  139.                 if (wnd->wlines)    {
  140.                     if (my > wnd->wlines-1)
  141.                         break;
  142.                     lp = TextLine(wnd, my);
  143.                     len = (int) (strchr(lp, '\n') - lp);
  144.                     mx = min(mx, len);
  145.                 }
  146.                 else
  147.                     mx = my = 0;
  148.  
  149.                 if (dragcount == -1)
  150.                     dragcount = 2;
  151.                 if (--dragcount == 0)    {
  152.                     SetAnchor(wnd, mx, my);
  153.                     TextMarking = TRUE;
  154.                     SendMessage(wnd, CAPTURE_MOUSE, TRUE, 0);
  155.                     return TRUE;
  156.                 }
  157.                 wnd->WndRow = my-wnd->wtop;
  158.                 SetLinePointer(wnd, my);
  159.             }
  160.             if (isMultiLine(wnd) || !BlockMarked(wnd))
  161.                 wnd->CurrCol = wnd->wleft+mx;
  162.             PostMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  163.             return TRUE;
  164.         case MOUSE_MOVED:
  165.             if (my > wnd->wlines-1)
  166.                 break;
  167.             if (TextMarking && !(WindowMoving || WindowSizing))    {
  168.                 int ptop;
  169.                 int pbot;
  170.                 char *lp;
  171.                 int len;
  172.                 int y;
  173.                 int bbl, bel;
  174.                 RECT rc = ClientRect(wnd);
  175.  
  176.                 if (!InsideRect(p1, p2, rc))    {
  177.                     if (p1 < GetClientLeft(wnd))
  178.                         p1 = GetClientLeft(wnd);
  179.                     if (p1 > GetClientRight(wnd))
  180.                         p1 = GetClientRight(wnd);
  181.                     if (p2 < GetClientTop(wnd))
  182.                         p2 = GetClientTop(wnd);
  183.                     if (p2 > GetClientBottom(wnd))
  184.                         p2 = GetClientBottom(wnd);
  185.                     SendMessage(NULLWND, MOUSE_CURSOR, p1, p2);
  186.                 }
  187.  
  188.                 mx = (int) p1 - GetClientLeft(wnd);
  189.                 my = (int) p2 - GetClientTop(wnd);
  190.  
  191.                 ptop = min(wnd->BlkBegLine, wnd->BlkEndLine);
  192.                 pbot = max(wnd->BlkBegLine, wnd->BlkEndLine);
  193.  
  194.                 lp = TextLine(wnd, wnd->wtop+my);
  195.                 len = (int) (strchr(lp, '\n') - lp);
  196.                 mx = min(mx, len-wnd->wleft);
  197.  
  198.                 wnd->BlkEndCol = mx+wnd->wleft;
  199.                 wnd->BlkEndLine = my+wnd->wtop;
  200.  
  201.                 bbl = min(wnd->BlkBegLine, wnd->BlkEndLine);
  202.                 bel = max(wnd->BlkBegLine, wnd->BlkEndLine);
  203.  
  204.                 while (ptop < bbl)    {
  205.                     WriteTextLine(wnd, NULL, ptop-wnd->wtop, FALSE);
  206.                     ptop++;
  207.                 }
  208.                 for (y = bbl; y <= bel; y++)
  209.                     WriteTextLine(wnd, NULL, y-wnd->wtop, FALSE);
  210.                 while (pbot > bel)    {
  211.                     WriteTextLine(wnd, NULL, pbot-wnd->wtop, FALSE);
  212.                     --pbot;
  213.                 }
  214.                 return FALSE;
  215.             }
  216.             break;
  217.         case BUTTON_RELEASED:
  218.             if (!isMultiLine(wnd))
  219.                 break;
  220.             dragcount = -1;
  221.             if (TextMarking && !(WindowMoving || WindowSizing))    {
  222.                 PostMessage(wnd, RELEASE_MOUSE, 0, 0);
  223.                 TextMarking = FALSE;
  224.                 if (wnd->BlkBegLine > wnd->BlkEndLine)    {
  225.                     swap(wnd->BlkBegLine, wnd->BlkEndLine);
  226.                     swap(wnd->BlkBegCol, wnd->BlkEndCol);
  227.                 }
  228.                 if (wnd->BlkBegLine == wnd->BlkEndLine &&
  229.                         wnd->BlkBegCol > wnd->BlkEndCol)
  230.                     swap(wnd->BlkBegCol, wnd->BlkEndCol);
  231.                 return TRUE;
  232.             }
  233.             else
  234.                 py = -1;
  235.             break;
  236.         case SCROLL:
  237.             if (!isMultiLine(wnd))
  238.                 break;
  239.             if ((rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2)) != FALSE)    {
  240.                 if (p1)    {
  241.                     /* -------- scrolling up --------- */
  242.                     if (wnd->WndRow == 0)    {
  243.                         DownLine(wnd);
  244.                         StickEnd(wnd);
  245.                     }
  246.                     else
  247.                         --wnd->WndRow;
  248.                 }
  249.                 else    {
  250.                     /* -------- scrolling down --------- */
  251.                     if (wnd->WndRow == ClientHeight(wnd)-1)    {
  252.                         UpLine(wnd);
  253.                         StickEnd(wnd);
  254.                     }
  255.                     else
  256.                         wnd->WndRow++;
  257.                 }
  258.                 SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  259.             }
  260.             return rtn;
  261.         case HORIZSCROLL:
  262.             if (p1 && wnd->CurrCol == wnd->wleft &&
  263.                     *CurrChar == '\n')
  264.                 return FALSE;
  265.             if ((rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2)) != FALSE)    {
  266.                 if (wnd->CurrCol < wnd->wleft)
  267.                     wnd->CurrCol++;
  268.                 else if (WndCol == ClientWidth(wnd))
  269.                     --wnd->CurrCol;
  270.                 SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  271.             }
  272.             return rtn;
  273.         case MOVE:
  274.             rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  275.             SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  276.             return rtn;
  277.         case SIZE:
  278.             rtn = BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  279.             if (WndCol > ClientWidth(wnd)-1)
  280.                 wnd->CurrCol = ClientWidth(wnd)-1 + wnd->wleft;
  281.             if (wnd->WndRow > ClientHeight(wnd)-1)    {
  282.                 wnd->WndRow = ClientHeight(wnd)-1;
  283.                 SetLinePointer(wnd, wnd->WndRow+wnd->wtop);
  284.             }
  285.             SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  286.             return rtn;
  287.         case KEYBOARD:
  288.             if (WindowMoving || WindowSizing)
  289.                 break;
  290.             c = (int) p1;
  291.             if ((p2 & ALTKEY) || 
  292.                     c == SHIFT_INS || c == SHIFT_DEL)
  293.                 break;
  294.             if ((p2 & CTRLKEY) && c != CTRL_FWD  &&
  295.                                   c != CTRL_BS   &&
  296.                                   c != CTRL_HOME &&
  297.                                   c != CTRL_END)
  298.                 break;
  299.             if (c == ESC)
  300.                 break;
  301.             if (isMultiLine(wnd))    {
  302.                 if (p2 & (LEFTSHIFT | RIGHTSHIFT))    {
  303.                     SendMessage(NULLWND, CURRENT_KEYBOARD_CURSOR,
  304.                         (PARAM) &kx, (PARAM)&ky);
  305.  
  306.                     kx -= GetClientLeft(wnd);
  307.                     ky -= GetClientTop(wnd);
  308.  
  309.                     switch (c)    {
  310.                         case HOME:
  311.                         case END:
  312.                         case PGUP:
  313.                         case PGDN:
  314.                         case UP:
  315.                         case DN:
  316.                         case FWD:
  317.                         case BS:
  318.                         case CTRL_FWD:
  319.                         case CTRL_BS:
  320.                             if (!KeyBoardMarking)    {
  321.                                 if (BlockMarked(wnd))    {
  322.                                     ClearBlock(wnd);
  323.                                     SendMessage(wnd, PAINT, 0, 0);
  324.                                 }
  325.                                 KeyBoardMarking = TextMarking = TRUE;
  326.                                 SetAnchor(wnd, kx+wnd->wleft, ky+wnd->wtop);
  327.                             }
  328.                             break;
  329.                         default:
  330.                             break;
  331.                     }
  332.                 }
  333.                 else if (((c != DEL && c != RUBOUT) ||
  334.                         !isMultiLine(wnd)) && BlockMarked(wnd))    {
  335.                     ClearBlock(wnd);
  336.                     SendMessage(wnd, PAINT, 0, 0);
  337.                 }
  338.             }
  339.             switch (c)    {
  340.                 case PGUP:
  341.                 case PGDN:
  342.                     if (!isMultiLine(wnd))
  343.                         break;
  344.                     BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  345.                     SetLinePointer(wnd, wnd->wtop+wnd->WndRow);
  346.                     StickEnd(wnd);
  347.                     SendMessage(wnd, KEYBOARD_CURSOR,WndCol, wnd->WndRow);
  348.                     break;
  349.                 case HOME:
  350.                     Home(wnd);
  351.                     break;
  352.                 case END:
  353.                     End(wnd);
  354.                     break;
  355.                 case CTRL_FWD:
  356.                     NextWord(wnd);
  357.                     break;
  358.                 case CTRL_BS:
  359.                     PrevWord(wnd);
  360.                     break;
  361.                 case CTRL_HOME:
  362.                     if (!isMultiLine(wnd))    {
  363.                         Home(wnd);
  364.                         break;
  365.                     }
  366.                     rtn = BaseWndProc(EDITBOX, wnd, msg, HOME, p2);
  367.                     Home(wnd);
  368.                     wnd->CurrLine = wnd->text;
  369.                     wnd->WndRow = 0;
  370.                     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  371.                     return rtn;
  372.                 case CTRL_END:
  373.                     if (!isMultiLine(wnd))    {
  374.                         End(wnd);
  375.                         break;
  376.                     }
  377.                     Home(wnd);
  378.                     rtn = BaseWndProc(EDITBOX, wnd, msg, END, p2);
  379.                     SetLinePointer(wnd, wnd->wlines-1);
  380.                     wnd->WndRow = min(ClientHeight(wnd)-1, wnd->wlines-1);
  381.                     End(wnd);
  382.                     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  383.                     return rtn;
  384.                 case UP:
  385.                     if (!isMultiLine(wnd))
  386.                         break;
  387.                     Upward(wnd);
  388.                     break;
  389.                 case DN:
  390.                     if (!isMultiLine(wnd))
  391.                         break;
  392.                     Downward(wnd);
  393.                     break;
  394.                 case FWD:
  395.                     Forward(wnd);
  396.                     break;
  397.                 case BS:
  398.                     Backward(wnd);
  399.                     break;
  400.             }
  401.             if (KeyBoardMarking)    {
  402.                 SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  403.                 SendMessage(NULLWND, CURRENT_KEYBOARD_CURSOR,
  404.                     (PARAM) &kx, (PARAM)&ky);
  405.                 SendMessage(wnd, MOUSE_MOVED, kx, ky);
  406.                 return TRUE;
  407.             }
  408.             if (!TestAttribute(wnd, READONLY))    {
  409.                 switch (c)    {
  410.                     case HOME:
  411.                     case END:
  412.                     case PGUP:
  413.                     case PGDN:
  414.                     case UP:
  415.                     case DN:
  416.                     case FWD:
  417.                     case BS:
  418.                     case CTRL_FWD:
  419.                     case CTRL_BS:
  420.                     case CTRL_HOME:
  421.                     case CTRL_END:
  422.                     case RUBOUT:
  423.                         if (!isMultiLine(wnd) && BlockMarked(wnd))    {
  424.                             ClearBlock(wnd);
  425.                             SendMessage(wnd, PAINT, 0, 0);
  426.                         }
  427.                         if (c != RUBOUT)
  428.                             break;
  429.                         if (wnd->CurrLine == wnd->text && wnd->CurrCol == 0)
  430.                             break;
  431.                         Backward(wnd);
  432.                         SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  433.                     case DEL:
  434.                         if (BlockMarked(wnd))    {
  435.                             SendMessage(wnd, COMMAND, ID_DELETETEXT, 0);
  436.                             return TRUE;
  437.                         }
  438.                         if (*(CurrChar+1) == '\0')
  439.                             return TRUE;
  440.                         if (isMultiLine(wnd) &&
  441.                                 *CurrChar == '\n')
  442.                             --wnd->wlines;
  443.                         if (*(CurrChar+1))    {
  444.                             int repaint = *CurrChar == '\n';
  445.                             strcpy(CurrChar, CurrChar+1);
  446.                             if (repaint)
  447.                                 SendMessage(wnd, PAINT, 0, 0);
  448.                             else
  449.                                 WriteTextLine(wnd, NULL, wnd->WndRow, FALSE);
  450.                         }
  451.                         wnd->TextChanged = TRUE;
  452.                         break;
  453.                     case INS:
  454.                         InvertCommandToggle(ID_INSERT);
  455.                         SendMessage(NULLWND, SHOW_CURSOR, GetCommandToggle(ID_INSERT), 0);
  456.                         break;
  457.                     case '\r':
  458.                         if (isMultiLine(wnd))
  459.                             c = '\n';
  460.                     default:
  461.                         if (c == '\t')    {
  462.                             int insmd = GetCommandToggle(ID_INSERT);
  463.                             if (!isMultiLine(wnd))
  464.                                 PostMessage(GetParent(wnd), msg, p1, p2);
  465.                             else do    {
  466.                                 if (!insmd && *(CurrChar+1) == '\0')
  467.                                     break;
  468.                                 SendMessage(wnd, KEYBOARD,
  469.                                     insmd ? ' ' : FWD, 0);
  470.                             } while (wnd->CurrCol % cfg.Tabs);
  471.                             return TRUE;
  472.                         }
  473.                         if ((c != '\n' && c < ' ') || (c & 0x1000))
  474.                             /* ---- not recognized by editor --- */
  475.                             break;
  476.                         if (!isMultiLine(wnd) && BlockMarked(wnd))    {
  477.                             ResetEditBox(wnd);
  478.                             ClearBlock(wnd);
  479.                         }
  480.                         if (*CurrChar == '\0')    {
  481.                             *CurrChar = '\n';
  482.                             *(CurrChar+1) = '\0';
  483.                             wnd->wlines++;
  484.                         }
  485.                         /* --- displayable char or newline --- */
  486.                         if (c == '\n' ||
  487.                                 GetCommandToggle(ID_INSERT) ||
  488.                                     *CurrChar == '\n')    {
  489.                             char *cp = wnd->CurrLine;
  490.                             while (*cp && *cp != '\n')
  491.                                 cp++;
  492.                             wnd->textwidth = max(wnd->textwidth,
  493.                                         (int) (cp-wnd->CurrLine+1));
  494.                             /* ------ insert mode ------ */
  495.                             memmove(CurrChar+1, CurrChar, strlen(CurrChar)+1);
  496.                             WriteTextLine(wnd, NULL, wnd->WndRow, FALSE);
  497.                             if (strlen(wnd->text) == wnd->textlen)    {
  498.                                 int dif = (int) (wnd->CurrLine - wnd->text);
  499.                                 wnd->textlen += GROWLENGTH;
  500.                                 wnd->text = realloc(wnd->text, wnd->textlen);
  501.                                 wnd->CurrLine = wnd->text + dif;
  502.                             }
  503.                         }
  504.                         /* ----- put the char in the buffer ----- */
  505.                         *CurrChar = c;
  506.                         wnd->TextChanged = TRUE;
  507.                         if (c == '\n')    {
  508.                             wnd->wleft = 0;
  509.                             wnd->wlines++;
  510.                             End(wnd);
  511.                             Forward(wnd);
  512.                             SendMessage(wnd, PAINT, 0, 0);
  513.                             break;
  514.                         }
  515.                         /* ---------- test end of window --------- */
  516.                         if (WndCol == ClientWidth(wnd)-1)    {
  517.                             int dif;
  518.                             char *cp = CurrChar;
  519.                             while (*cp != ' ' && cp != wnd->CurrLine)
  520.                                 --cp;
  521.                             if (!isMultiLine(wnd) || cp == wnd->CurrLine ||
  522.                                     !GetCommandToggle(ID_WRAP))
  523.                                 SendMessage(wnd, HORIZSCROLL, TRUE, 0);
  524.                             else    {
  525.                                 dif = 0;
  526.                                 if (c != ' ')    {
  527.                                     dif = (int) (CurrChar - cp);
  528.                                     wnd->CurrCol -= dif;
  529.                                     SendMessage(wnd, KEYBOARD, DEL, 0);
  530.                                     --dif;
  531.                                 }
  532.                                 SendMessage(wnd, KEYBOARD, '\r', 0);
  533.                                 wnd->CurrCol = dif;
  534.                                 if (c == ' ')
  535.                                     break;
  536.                             }
  537.                         }
  538.                         /* ------ display the character ------ */
  539.                         SetStandardColor(wnd);
  540.                         PutWindowChar(wnd, WndCol, wnd->WndRow, c);
  541.                         /* ----- advance the pointers ------ */
  542.                         wnd->CurrCol++;
  543.                         break;
  544.                 }
  545.             }
  546.             SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  547.             return TRUE;
  548.         case COMMAND:    {
  549.             char *bbl, *bel, *bb;
  550.             int len;
  551.  
  552.             if (BlockMarked(wnd))    {
  553.                 bbl = FindLinePointer(wnd, wnd->BlkBegLine) + wnd->BlkBegCol;
  554.                 bel = FindLinePointer(wnd, wnd->BlkEndLine) + wnd->BlkEndCol;
  555.                 len = (int) (bel - bbl);
  556.             }
  557.             switch ((int)p1)    {
  558.                 case ID_CUT:
  559.                 case ID_COPY:
  560.                     ClipboardLength = len;
  561.                     Clipboard = realloc(Clipboard, ClipboardLength);
  562.                     if (Clipboard != NULL)
  563.                         memmove(Clipboard, bbl, ClipboardLength);
  564.                 case ID_DELETETEXT:
  565.                     if (p1 != ID_COPY)    {
  566.                         if (p1 != ID_CUT)
  567.                             SaveDeletedText(wnd, bbl, len);
  568.                         wnd->TextChanged = TRUE;
  569.                         strcpy(bbl, bel);
  570.                         wnd->CurrLine = bbl - wnd->BlkBegCol;
  571.                         wnd->CurrCol = wnd->BlkBegCol;
  572.                         wnd->WndRow = wnd->BlkBegLine - wnd->wtop;
  573.                         if (wnd->WndRow < 0)    {
  574.                             wnd->wtop = wnd->BlkBegLine;
  575.                             wnd->WndRow = 0;
  576.                         }
  577.                         wnd->wlines -= wnd->BlkEndLine - wnd-> BlkBegLine;
  578.                         SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  579.                     }
  580.                     ClearBlock(wnd);
  581.                     SendMessage(wnd, PAINT, 0, 0);
  582.                     return TRUE;
  583.                 case ID_CLEAR:
  584.                     SaveDeletedText(wnd, bbl, len);
  585.                     wnd->CurrLine = bbl;
  586.                     wnd->CurrCol = wnd->BlkBegCol;
  587.                     wnd->WndRow = wnd->BlkBegLine - wnd->wtop;
  588.                     if (wnd->WndRow < 0)    {
  589.                         wnd->WndRow = 0;
  590.                         wnd->wtop = wnd->BlkBegLine;
  591.                     }
  592.                     while (bbl < bel)    {
  593.                         char *cp = strchr(bbl, '\n');
  594.                         if (cp > bel)
  595.                             cp = bel;
  596.                         strcpy(bbl, cp);
  597.                         bel -= (int) (cp - bbl);
  598.                         bbl++;
  599.                     }
  600.                     ClearBlock(wnd);
  601.                     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  602.                     SendMessage(wnd, PAINT, 0, 0);
  603.                     wnd->TextChanged = TRUE;
  604.                     return TRUE;
  605.                 case ID_PASTE:
  606.                     if (Clipboard != NULL)    {
  607.                         PasteText(wnd, Clipboard, ClipboardLength);
  608.                         wnd->TextChanged = TRUE;
  609.                     }
  610.                     return TRUE;
  611.                 case ID_UNDO:
  612.                     if (wnd->DeletedText != NULL)    {
  613.                         PasteText(wnd, wnd->DeletedText, wnd->DeletedLength);
  614.                         free(wnd->DeletedText);
  615.                         wnd->DeletedText = NULL;
  616.                     }
  617.                     return TRUE;
  618.                 case ID_PARAGRAPH:    {
  619.                     char *bl;
  620.                     int bc, ec;
  621.                     int fl, el;
  622.                     int Blocked;
  623.  
  624.                     el = wnd->BlkEndLine;
  625.                     ec = wnd->BlkEndCol;
  626.                     if (!BlockMarked(wnd))    {
  627.                         Blocked = FALSE;
  628.                         /* ---- forming paragraph from cursor position --- */
  629.                         fl = wnd->wtop + wnd->WndRow;
  630.                         bl = wnd->CurrLine;
  631.                         bc = wnd->CurrCol;
  632.                         Home(wnd);
  633.                         bbl = bel = bl;
  634.                         if (bc >= ClientWidth(wnd))
  635.                             bc = 0;
  636.                         /* ---- locate the end of the paragraph ---- */
  637.                         while (*bel)    {
  638.                             int blank = TRUE;
  639.                             char *bll = bel;
  640.                             /* --- blank line marks end of paragraph --- */
  641.                             while (*bel && *bel != '\n')    {
  642.                                 if (*bel != ' ')
  643.                                     blank = FALSE;
  644.                                 bel++;
  645.                             }
  646.                             if (blank)    {
  647.                                 bel = bll;
  648.                                 break;
  649.                             }
  650.                             if (*bel)
  651.                                 bel++;
  652.                         }
  653.                         if (bel == bbl)    {
  654.                             SendMessage(wnd, KEYBOARD, DN, 0);
  655.                             return TRUE;
  656.                         }
  657.                         if (*bel == '\0')
  658.                             --bel;
  659.                         if (*bel == '\n')
  660.                             --bel;
  661.                     }
  662.                     else    {
  663.                         Blocked = TRUE;
  664.                         /* ---- forming paragraph from marked block --- */
  665.                         fl = wnd->BlkBegLine;
  666.                         bc = wnd->CurrCol = wnd->BlkBegCol;
  667.                         wnd->CurrLine = FindLinePointer(wnd, fl);
  668.                         if (fl < wnd->wtop)
  669.                             wnd->wtop = fl;
  670.                         wnd->WndRow = fl - wnd->wtop;
  671.                         SendMessage(wnd, KEYBOARD, '\r', 0);
  672.                         el++, fl++;
  673.                         if (bc != 0)    {
  674.                             SendMessage(wnd, KEYBOARD, '\r', 0);
  675.                             el++, fl ++;
  676.                         }
  677.                         bc = 0;
  678.                         bl = wnd->CurrLine = FindLinePointer(wnd, fl);
  679.                         bbl = bl + bc;
  680.                         bel = FindLinePointer(wnd, el) + ec;
  681.                     }
  682.  
  683.                     /* --- change all newlines in block to spaces --- */
  684.                     while (CurrChar < bel)    {
  685.                         if (*CurrChar == '\n')    {
  686.                             *CurrChar = ' ';
  687.                             wnd->CurrLine = CurrChar + 1;
  688.                             wnd->CurrCol = 0;
  689.                             --wnd->wlines;
  690.                         }
  691.                         else
  692.                             wnd->CurrCol++;
  693.                     }
  694.  
  695.                     /* ---- insert newlines at new margin boundaries ---- */
  696.                     bb = bbl;
  697.                     while (bbl < bel)    {
  698.                         bbl++;
  699.                         if ((int)(bbl - bb) == ClientWidth(wnd)-1)    {
  700.                             while (*bbl != ' ' && bbl > bb)
  701.                                 --bbl;
  702.                             if (*bbl != ' ')    {
  703.                                 bbl = strchr(bbl, ' ');
  704.                                 if (bbl == NULL || bbl >= bel)
  705.                                     break;
  706.                             }
  707.                             *bbl = '\n';
  708.                             wnd->textwidth = max(wnd->textwidth,
  709.                                 (int) (bbl-bb));
  710.                             wnd->wlines++;
  711.                             bb = bbl+1;
  712.                         }
  713.                     }
  714.                     ec = (int)(bel - bb);
  715.  
  716.                     if (Blocked)    {
  717.                         /* ---- position cursor at end of new paragraph ---- */
  718.                         if (el < wnd->wtop ||
  719.                                 wnd->wtop + ClientHeight(wnd) < el)
  720.                             wnd->wtop = el-ClientHeight(wnd);
  721.                         if (wnd->wtop < 0)
  722.                             wnd->wtop = 0;
  723.                         wnd->WndRow = el - wnd->wtop;
  724.                         wnd->CurrLine = FindLinePointer(wnd, el);
  725.                         wnd->CurrCol = ec;
  726.                         SendMessage(wnd, KEYBOARD, '\r', 0);
  727.                         SendMessage(wnd, KEYBOARD, '\r', 0);
  728.                     }
  729.                     else    {
  730.                         /* --- put cursor back at beginning --- */
  731.                         wnd->CurrLine = bl;
  732.                         wnd->CurrCol = bc;
  733.                         if (fl < wnd->wtop)
  734.                             wnd->wtop = fl;
  735.                         wnd->WndRow = fl - wnd->wtop;
  736.                     }
  737.                     SendMessage(wnd, PAINT, 0, 0);
  738.                     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  739.                     wnd->TextChanged = TRUE;
  740.                     return TRUE;
  741.                 }
  742.                 default:
  743.                     break;
  744.             }
  745.             break;
  746.         }
  747.         case CLOSE_WINDOW:
  748.             SendMessage(NULLWND, HIDE_CURSOR, 0, 0);
  749.             if (wnd->DeletedText != NULL)
  750.                 free(wnd->DeletedText);
  751.             break;
  752.         default:
  753.             break;
  754.     }
  755.     return BaseWndProc(EDITBOX, wnd, msg, p1, p2);
  756. }
  757.  
  758. static void PasteText(WINDOW wnd, char *SaveTo, int len)
  759. {
  760.     int plen = strlen(wnd->text) + len + 1;
  761.     char *bl = CurrChar;
  762.     char *el = bl+len;
  763.     if (plen > wnd->textlen)    {
  764.         wnd->text = realloc(wnd->text, plen);
  765.         wnd->textlen = plen;
  766.     }
  767.     memmove(el,    bl,    strlen(bl)+1);
  768.     memmove(bl, SaveTo, len);
  769.     while (bl < el)
  770.         if (*bl++ == '\n')
  771.             wnd->wlines++;
  772.     SendMessage(wnd, PAINT, 0, 0);
  773. }
  774.  
  775. static void SaveDeletedText(WINDOW wnd, char *bbl, int len)
  776. {
  777.     wnd->DeletedLength = len;
  778.     if ((wnd->DeletedText = realloc(wnd->DeletedText, len)) != NULL)
  779.         memmove(wnd->DeletedText, bbl, len);
  780. }
  781.  
  782. static void SetAnchor(WINDOW wnd, int mx, int my)
  783. {
  784.     if (BlockMarked(wnd))    {
  785.         ClearBlock(wnd);
  786.         SendMessage(wnd, PAINT, 0, 0);
  787.     }
  788.     dragcount = -1;
  789.     /* ------ set the anchor ------ */
  790.     wnd->BlkBegLine = wnd->BlkEndLine = my;
  791.     wnd->BlkBegCol = wnd->BlkEndCol = mx;
  792.     WriteTextLine(wnd, NULL, my, FALSE);
  793. }
  794.  
  795. static void Forward(WINDOW wnd)
  796. {
  797.     if (*(CurrChar+1) == '\0')
  798.         return;
  799.     if (*CurrChar == '\n')    {
  800.         Home(wnd);
  801.         Downward(wnd);
  802.     }
  803.     else    {
  804.         wnd->CurrCol++;
  805.         if (WndCol == ClientWidth(wnd))    {
  806.             wnd->wleft++;
  807.             SendMessage(wnd, PAINT, 0, 0);
  808.         }
  809.     }
  810. }
  811.  
  812. static void StickEnd(WINDOW wnd)
  813. {
  814.     int len = (int) (strchr(wnd->CurrLine, '\n') - wnd->CurrLine);
  815.  
  816.     wnd->CurrCol = min(len, wnd->CurrCol);
  817.     if (wnd->wleft > wnd->CurrCol)    {
  818.         wnd->wleft = max(0, wnd->CurrCol - 4);
  819.         SendMessage(wnd, PAINT, 0, 0);
  820.     }
  821.     else if (wnd->CurrCol-wnd->wleft >= ClientWidth(wnd))    {
  822.         wnd->wleft = wnd->CurrCol - (ClientWidth(wnd)-1);
  823.         SendMessage(wnd, PAINT, 0, 0);
  824.     }
  825. }
  826.  
  827. static void Downward(WINDOW wnd)
  828. {
  829.     if (isMultiLine(wnd) && wnd->WndRow+wnd->wtop+1 < wnd->wlines)    {
  830.         DownLine(wnd);
  831.         if (wnd->WndRow == ClientHeight(wnd)-1)    {
  832.             if (wnd->wtop+ClientHeight(wnd) <= wnd->wlines)    {
  833.                 wnd->wtop++;
  834.                 SendMessage(wnd, PAINT, 0, 0);
  835.             }
  836.         }
  837.         else
  838.             wnd->WndRow++;
  839.         StickEnd(wnd);
  840.     }
  841. }
  842.  
  843. static void DownLine(WINDOW wnd)
  844. {
  845.     wnd->CurrLine = strchr(wnd->CurrLine, '\n');
  846.     wnd->CurrLine++;
  847. }
  848.  
  849. static void UpLine(WINDOW wnd)
  850. {
  851.     --wnd->CurrLine;
  852.     if (wnd->CurrLine != wnd->text)    {
  853.         --wnd->CurrLine;
  854.         while (wnd->CurrLine != wnd->text && *wnd->CurrLine != '\n')
  855.             --wnd->CurrLine;
  856.         if (*wnd->CurrLine == '\n')
  857.             wnd->CurrLine++;
  858.     }
  859. }
  860.  
  861. static void Upward(WINDOW wnd)
  862. {
  863.     if (isMultiLine(wnd) && wnd->CurrLine != wnd->text)    {
  864.         UpLine(wnd);
  865.         if (wnd->WndRow == 0)    {
  866.             if (wnd->wtop)    {
  867.                 --wnd->wtop;
  868.                 SendMessage(wnd, PAINT, 0, 0);
  869.             }
  870.         }
  871.         else
  872.             --wnd->WndRow;
  873.         StickEnd(wnd);
  874.     }
  875. }
  876.  
  877. static void Backward(WINDOW wnd)
  878. {
  879.     if (wnd->CurrCol)    {
  880.         if (wnd->CurrCol-- <= wnd->wleft)    {
  881.             if (wnd->wleft != 0)    {
  882.                 --wnd->wleft;
  883.                 SendMessage(wnd, PAINT, 0, 0);
  884.                 return;
  885.             }
  886.         }
  887.         else
  888.             return ;
  889.     }
  890.     if (isMultiLine(wnd) && wnd->CurrLine != wnd->text)    {
  891.         Upward(wnd);
  892.         End(wnd);
  893.     }
  894. }
  895.  
  896. static void End(WINDOW wnd)
  897. {
  898.     while (*CurrChar != '\n')
  899.         ++wnd->CurrCol;
  900.     if (WndCol >= ClientWidth(wnd))    {
  901.         wnd->wleft = wnd->CurrCol - (ClientWidth(wnd)-1);
  902.         SendMessage(wnd, PAINT, 0, 0);
  903.     }
  904. }
  905.  
  906. static void Home(WINDOW wnd)
  907. {
  908.     wnd->CurrCol = 0;
  909.     if (wnd->wleft != 0)    {
  910.         wnd->wleft = 0;
  911.         SendMessage(wnd, PAINT, 0, 0);
  912.     }
  913. }
  914.  
  915. static char *FindLinePointer(WINDOW wnd, int ln)
  916. {
  917.     char *cp = wnd->text;
  918.     while (ln--)    {
  919.         cp = strchr(cp, '\n');
  920.         cp++;
  921.     }
  922.     return cp;
  923. }
  924.  
  925. #define isWhite(c)     ((c) == ' ' || (c) == '\n')
  926.  
  927. static void NextWord(WINDOW wnd)
  928. {
  929.     int savetop = wnd->wtop;
  930.     int saveleft = wnd->wleft;
  931.     ClearVisible(wnd);
  932.     while (!isWhite(*CurrChar))    {
  933.         if (*(CurrChar+1) == '\0')
  934.             break;
  935.         Forward(wnd);
  936.     }
  937.     while (isWhite(*CurrChar))    {
  938.         if (*(CurrChar+1) == '\0')
  939.             break;
  940.         Forward(wnd);
  941.     }
  942.     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  943.     SetVisible(wnd);
  944.     if (wnd->wtop != savetop || wnd->wleft != saveleft)
  945.         SendMessage(wnd, PAINT, 0, 0);
  946. }
  947.  
  948. static void PrevWord(WINDOW wnd)
  949. {
  950.     int savetop = wnd->wtop;
  951.     int saveleft = wnd->wleft;
  952.     ClearVisible(wnd);
  953.     Backward(wnd);
  954.     while (isWhite(*CurrChar))    {
  955.         if (wnd->CurrLine == wnd->text && wnd->CurrCol == 0)
  956.             break;
  957.         Backward(wnd);
  958.     }
  959.     while (!isWhite(*CurrChar))    {
  960.         if (wnd->CurrLine == wnd->text && wnd->CurrCol == 0)
  961.             break;
  962.         Backward(wnd);
  963.     }
  964.     if (isWhite(*CurrChar))
  965.         Forward(wnd);
  966.     if (wnd->wleft != saveleft)
  967.         if (wnd->CurrCol >= saveleft)
  968.             if (wnd->CurrCol - saveleft < ClientWidth(wnd))
  969.                 wnd->wleft = saveleft;
  970.     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  971.     SetVisible(wnd);
  972.     if (wnd->wtop != savetop || wnd->wleft != saveleft)
  973.         SendMessage(wnd, PAINT, 0, 0);
  974. }
  975.  
  976. static void ResetEditBox(WINDOW wnd)
  977. {
  978.     *wnd->text = '\0';
  979.     wnd->wlines = 0;
  980.     wnd->CurrLine = wnd->text;
  981.     wnd->CurrCol = 0;
  982.     wnd->WndRow = 0;
  983.     wnd->TextChanged = FALSE;
  984.     wnd->wleft = 0;
  985. }
  986.  
  987.